﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using System;
	using Falcon.Core.Authentication;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserAlbum : EntityBase<UserAlbum>
	{
		// private fields
		private User _user;
		private UserAlbumPhoto _preview;

		/// <summary>
		/// Initializes a new instance of the <see cref="UserAlbum"/> class.
		/// </summary>
		public UserAlbum()
		{
			this.Identity = User.Identity;
			base.DeterminePermission(Roles.UsersAlbums);
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public virtual int UserId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the preview id.
		/// </summary>
		/// <value>The preview id.</value>
		public virtual int PreviewId
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public virtual string Title
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the slug.
		/// </summary>
		/// <value>The slug.</value>
		public virtual string Slug
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public virtual string Body
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the body parsed.
		/// </summary>
		/// <value>The body parsed.</value>
		public virtual string BodyParsed
		{
			get;
			set;
		}

		/// <summary>
		/// Gets a value indicating whether this instance is owner.
		/// </summary>
		/// <value><c>true</c> if this instance is owner; otherwise, <c>false</c>.</value>
		public virtual bool IsOwner
		{
			get
			{
				return this.Identity != null && this.UserId.Equals(this.Identity.Id);
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is contact.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is contact; otherwise, <c>false</c>.
		/// </value>
		public virtual bool IsContact
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can view.
		/// </summary>
		/// <value><c>true</c> if this instance can view; otherwise, <c>false</c>.</value>
		public override bool CanView
		{
			get
			{
				return base.CanView && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can update.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can update; otherwise, <c>false</c>.
		/// </value>
		public override bool CanUpdate
		{
			get
			{
				return base.CanUpdate && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can delete.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can delete; otherwise, <c>false</c>.
		/// </value>
		public override bool CanDelete
		{
			get
			{
				return base.CanDelete && this.IsOwner;
			}
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public virtual UserAlbumScope Scope
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the created.
		/// </summary>
		/// <value>The created.</value>
		public virtual DateTime Created
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the updated.
		/// </summary>
		/// <value>The updated.</value>
		public virtual DateTime Updated
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the user.
		/// </summary>
		/// <value>The user.</value>
		public virtual User User
		{
			get
			{
				return _user;
			}
			set
			{
				_user = value;

				if (_user != null)
				{
					this.UserId = _user.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the preview.
		/// </summary>
		/// <value>The preview.</value>
		public virtual UserAlbumPhoto Preview
		{
			get
			{
				return _preview;
			}
			set
			{
				_preview = value;

				if (_preview != null)
				{
					this.PreviewId = _preview.Id;
				}
			}
		}

		/// <summary>
		/// Gets or sets the identity.
		/// </summary>
		/// <value>The identity.</value>
		public virtual WebUserIdentity Identity
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the photos.
		/// </summary>
		/// <value>The photos.</value>
		public virtual IPagedList<UserAlbumPhoto, UserAlbumPhotoSpecification> Photos
		{
			get; 
			set;
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserAlbum> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			return rules;
		}

		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}